layui.use(['form','tree','element','table','laytpl','treeGrid','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;
    var _companyId = $.cookie("companyId");
    //列表数据
    var option = {
        elem: '#tableList',
        url : '/produce/batch/pageByCreateBy?createBy='+_companyId,
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 15,
        id : "listTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            /*{field: 'id', title: '批次编号', minWidth:100, align:"center"},*/
            {field: 'produceName', title: '产品批次名称', minWidth:100, align:"center"},
            {field: 'originNumber', title: '批次数量', minWidth:100, align:"center"},
            {field: 'finallyNumber', title: '最终出产数量', minWidth:100, align:"center"},
            {field: 'batchStatus', title: '状态', minWidth:100, align:"center", templet:function(d){
                   if (d.batchStatus == 0) {
                       return '<span style="color: red;">待生产</span>'
                   } else if(d.batchStatus == 1) {
                       return '<span style="color: dodgerblue;">生产中</span>'
                   } else {
                       return '<span>完成</span>'
                   }
                }},
            {field: 'responsiblePerson', title: '负责人', minWidth:100, align:"center"},
            {field: 'regulatoryAuthorities', title: '监管部门', minWidth:100, align:"center"},
            {title: '操作', minWidth:280, fixed:"right",align:"center",templet: function(d){
                    var addBtn='<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>';
                    var processBtn = '<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="product">投入生产</a>';
                    if (d.batchStatus == 1) {
                        processBtn = '<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="product_manager">生产管理</a>';
                    } else if(d.batchStatus == 2) {
                        processBtn = '<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="product_manager">生产管理</a>';
                    }
                    var addTraceLib = '<a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="addTraceLib">加入溯源库</a>';
                    var delBtn='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    return addBtn + processBtn + addTraceLib + delBtn;
                }}
        ]]
    };

    //产品批次列表
    var tableIns = table.render(option);

    //搜索产品批次信息
    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            option.url = '/produce/batch/query';
            option.method = 'post';
            option.where = {produceName: searchKey};
            table.render(option);
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加产品批次
    $(".addBtn").click(function(){
        var index = layui.layer.open({
            title : "添加产品批次",
            type : 2,
            content : "produceBatchAdd",
            area: ['100%',$(document).height() + 'px'],
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回产品批次列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        });
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('listTable'),//此处的id为table数据格式中的id
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].id);
            }
            layer.confirm('确定删除选中的产品批次？', {icon: 3, title: '提示信息'}, function (index) {
                for(var i=0;i<idArr.length;i++){
                    $.get("/produce/batch/delete?id=" + idArr[i],function(data){
                        if (data.code == 200) {
                            layer.msg("删除成功！");
                        } else {
                            layer.msg("删除失败，请重试！");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                }
            })
        }else{
            layer.msg("请选择需要删除的产品批次");
        }
    });

    //列表操作
    table.on('tool(tableList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            var index = layui.layer.open({
                title : "修改产品批次信息",
                type : 2,
                content : "produceBatchEdit?id=" + data.id,
                area: ['100%',$(document).height() - 30 + 'px'],
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回产品批次列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });
        }else if(layEvent === 'product') { //配置产品批次规格信息
            var index = layui.layer.open({
                title : "确定投入生产？",
                type : 2,
                area: ['400px','250px'],
                content : "comfirm?id=" + data.id,
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
        } else if(layEvent === 'product_manager'){
            //打开生产管理
            var index = layui.layer.open({
                title: "生产管理--当前批次：" + data.produceName,
                type: 2,
                content: "recordList2?id=" + data.id + "&processTemplateId=" + data.processTemplateId,
                area: ['100%',($(document).height() - 40) + 'px'],
                success: function () {
                    setTimeout(function () {
                        layui.layer.tips('点击此处返回产品批次列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize", function () {
                layui.layer.full(index);
            });
        } else if(layEvent === 'addTraceLib'){
            layer.confirm('确定加入溯源库？',{icon:3, title:'提示信息'},function(index){
                $.get("/produce/batch/edit?id=" + data.id + "&batchStatus=2",function(data){
                    if (data.code == 200) {
                        layer.msg("加入成功！");
                    } else {
                        layer.msg("加入失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                });
            });
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此产品批次？',{icon:3, title:'提示信息'},function(index){
                $.get("/produce/batch/delete?id=" + data.id,function(data){
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                });
            });
        }
    });

});